<?php
declare(strict_types=1);
/**
 * @project: 兔兔找图微信小程序
 * @author: kert
 * @date: 2023/5/16
 * @link: https://www.qqdeveloper.com/
 * @site: 微信搜索-兔兔找图
 */

namespace App\Logic\User\Service\Ai;

use App\Constant\CacheKey;
use App\Exceptions\ScoreException;
use App\Library\Ai\BaiDu\BaiDuApiImage;
use App\Library\RedisClient;
use App\Logic\User\Service\BaseService;
use App\Logic\User\Service\User\UserScoreHistoryService;
use App\Logic\User\Service\UserServiceInterface;
use Closure;
use RedisException;

class AiHistoryService extends BaseService implements UserServiceInterface
{

    public static function searchWhere(array $requestParams): Closure
    {
        return function ($query) use ($requestParams) {
            extract($requestParams);
        };
    }

    public function serviceSelect(array $requestParams): array
    {
        return [];
    }

    public function serviceCreate(array $requestParams): array
    {
        $createResult = [
            "code" => 2,
            "msg" => "积分操作失败",
        ];
        if (!RedisClient::client()->set(CacheKey::$aiLimit . $this->getUserUid(), 1, ["nx", "ex" => 10])) {
            $createResult["msg"] = "请求过快";
            return $createResult;
        }
        $createScoreHistory = (new UserScoreHistoryService())->serviceCreate([
            "score" => 200,
            "title" => "百度AI漫画",
            "type" => 2,
        ]);
        if ($createScoreHistory["code"] == 1) {
            $createResult = (new BaiDuApiImage())->imageProcess(
                $requestParams["imageUrl"],
                $requestParams["artistValue"] ?? "anime",
                $requestParams["maskId"] ?? 1
            );
            if ($createResult["code"] == 0) {
                // 数据添加到缓存队列，异步持久化数据库
                RedisClient::client()->lPush(CacheKey::AiImageProcessQueue, [
                    "origin_image_url" => $requestParams["imageUrl"],// 原图片
                    "result_image_url" => $createResult["url"],// 生成后的图片
                    "scene" => "baidu_ai_image_process",
                ]);
            }
        }
        return $createResult;
    }

    /**
     * 图像识别
     * @param array $requestParams
     * @return array
     * @throws ScoreException
     * @throws RedisException
     */
    public function serviceRecognition(array $requestParams): array
    {
        $createResult = [
            "code" => 2,
            "msg" => "积分操作失败",
        ];
        if (!RedisClient::client()->set(CacheKey::$aiLimit . $this->getUserUid(), 1, ["nx", "ex" => 10])) {
            $createResult["msg"] = "请求过快";
            return $createResult;
        }
        $createScoreHistory = (new UserScoreHistoryService())->serviceCreate([
            "score" => 100,
            "title" => "百度图像识别",
            "type" => 2,
        ]);
        if ($createScoreHistory["code"] == 1) {
            $scene = $requestParams["scene"];
            $imageUrl = $requestParams["imageUrl"];
            return [
                "code" => 0,
                "msg" => "请求成功",
                "scene" => $requestParams["scene"],
                "data" => (new BaiDuApiImage())->$scene($imageUrl),
            ];
        }
        return $createResult;
    }

    public function serviceUpdate(array $requestParams): array
    {
        return [];
    }

    public function serviceDelete(array $requestParams): int
    {
        return 0;
    }

    public function serviceFind(array $requestParams): array
    {
        return [];
    }
}
